/**
 * 
 */
package dao.versant;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Properties;

import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;

import dao.interfaces.IGeneriqueDAO;

/**
 * @author Admin
 * @param <T>
 *
 */
public abstract class VersantDAO<T> implements IGeneriqueDAO<T> {

	protected PersistenceManagerFactory pmf;
	protected PersistenceManager pm;
	
	public VersantDAO() {
        Properties p;
		try {
			p = loadProperties();
	        pmf = JDOHelper.getPersistenceManagerFactory(p);
	        pm = pmf.getPersistenceManager();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
	
    private Properties loadProperties() throws IOException {
        Properties p = new Properties();
        InputStream in = null;
        try {
            in = VersantDAO.class.getResourceAsStream("/versant.properties");
            if (in == null) {
                throw new IOException("versant.properties not on classpath");
            }
            p.load(in);
        } finally {
            if (in != null) in.close();
        }
        return p;
    }

	@Override
	public void create(T obj) {
        pm.currentTransaction().begin();
        pm.makePersistent(obj);
        pm.currentTransaction().commit();
	}

	@Override
	public void delete(T obj) {
        pm.currentTransaction().begin();
        pm.deletePersistent(obj);
        pm.currentTransaction().commit();
	}
	
}
